﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using MINERP.COM;
using MINERP.ComControls;
using MINERP.MODEL.FoundationModel;
using MINERP.MODEL.OverOrder;
using MINERP.BLL.OverOrderBLL;



namespace MINERP.OverOrder.Forms
{
    public partial class FrmOverOrder : FrmParent, IView.IFrmOverOrder
    {
        private Presenter.FrmOverOrderPresenter _presenter;
        public FrmOverOrder()
        {
           // _presenter = new MINERP.OverOrder.Presenter.FrmOverOrderPresenter(this);
            InitializeComponent();
            base.InitiAuthority();
        }




        private void FrmOverOrder_Load(object sender, EventArgs e)
        {
            init();
        }
        private void init()
        {
            //OperateUserId = 2;//测试用test
            //SelectedOrderProductidList = new List<int>();
            comboBoxEdit_type.Properties.Items.AddRange(new string[] { "分片完成", "外加工中" });
            this.comboBoxEdit_type.SelectedIndex = 0;
            SelectedOrderInfoStatus = 3;
            ShowMainColums();

            labelControl1.Visible = false;
            labelContro_productType.Visible = false;
            labelControl2.Visible = false;
            labelControl5.Visible = false;
            labelControl4.Visible = false;
            labelControl6.Visible = false;
            labelControl7.Visible = false;

            // splitContainerControl_Main.PanelVisibility = SplitPanelVisibility.Panel1;
             _presenter = new MINERP.OverOrder.Presenter.FrmOverOrderPresenter(this);
            //simpleButton_OutProduct.Visible = true;
            //simpleButton_OverOrder.Visible = true;
            //simpleButton_ReceiveOrder.Visible = false;
            lookUpEditoutShop.Visible = false;
            GetSelectMainProductId();
            if (SelectedOrderInfoStatus == 3)
            {
                _presenter.ShowOutShopView();
                // ShowOutShopView();
                // _presenter.SelectOutShop();
            }
        }

        private void simpleButton_OutProduct_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            splitContainerControl_Main.PanelVisibility = SplitPanelVisibility.Both;
            _presenter.ShowOutShopView();
            this.Cursor = Cursors.Default;
        }

        private void simpleButton1_factoryClose_Click(object sender, EventArgs e)
        {
            splitContainerControl_Main.PanelVisibility = SplitPanelVisibility.Panel1;
        }

        /// <summary>
        /// 取得选择委外厂商的详细信息
        /// </summary>
        /// <param name="shopId"></param>
        private void DisplayShopInfo(int shopId)
        {
            this.SelectedOutShopId = shopId;
            ///显示已经委外给该厂商的产品列表
        }

        #region IFrmOverOrder 成员

        public int OperateUserId
        {
            get
            {
                return base.userId;
            }
            set
            {
                base.userId = value;
            }
            //set;
            //get;
        }

        public int SelectedOutShopId
        {
            get;
            set;
        }

        public int SelectedOrderInfoStatus
        {
            get;
            set;

        }

        public List<int> SelectedOrderProductidList
        {
            //set;
            get
            {
                List<int> orderInfoIdList = new List<int>();
                int pId;
                foreach (int rowIndex in gridView_Main.GetSelectedRows())
                {
                    if (!IsOuting)
                    {
                        pId = Convert.ToInt32(((OverOrderView)(gridView_Main.GetRow(rowIndex))).OrderProductInfo.OrderProductId);
                        orderInfoIdList.Add(pId);
                    }
                    else
                    {
                        //gridView_Main.GetRow(rowIndex)
                        pId =
                            Convert.ToInt32(((DataRowView)(gridView_Main.GetRow(rowIndex)))["OrderProductId"].ToString());
                        orderInfoIdList.Add(pId);

                    }
                }
                return orderInfoIdList;
            }
        }

        public void ShowMainView(List<MINERP.MODEL.OverOrder.OverOrderView> mainView)
        {
            gridControlMain.DataSource = mainView;
        }

        public void ShowShopTaskView(List<MINERP.MODEL.OverOrder.OverOrderView> shopTaskView)
        {
            gridControl_product.DataSource = shopTaskView;
        }


        public void ShowOutShopView(List<MINERP.MODEL.FoundationModel.OutProcessShop_Info> outShopView)
        {
            flowLayoutPanel_Shop.Controls.Clear();
            foreach (OutProcessShop_Info shopView in outShopView)
            {
                UOutShopInfo shopInfo = new UOutShopInfo();
                shopInfo.ShopId = shopView.OutShopId;
                shopInfo.ShopName = shopView.OutShopName;
                shopInfo.ShopCode = shopView.Code;

                shopInfo.DisplayInfo();
                shopInfo.GetSelectedShopId += new UOutShopInfo.DoSelectShop(shopInfo_GetSelectedShopId);//获得当前委外厂商Id 并显示委外产品信息
                shopInfo.ExecuteDrag += new UOutShopInfo.DoDrag(shopInfo_ExecuteDrag);
                shopInfo.SetSelectedShop += new UOutShopInfo.DoSelectShop(shopInfo_SetSelectedShop);
                shopInfo.ClearStyle += new UOutShopInfo.DoStyle(shopInfo_ClearStyle);
                flowLayoutPanel_Shop.Controls.Add(shopInfo);
            }
        }

        void shopInfo_ClearStyle()
        {
            foreach (Control uc in flowLayoutPanel_Shop.Controls)
            {
                ((UOutShopInfo)uc).BorderStyle = BorderStyle.FixedSingle;
                ((UOutShopInfo)uc).BackColor = Color.White;
            }
        }

        public string InFetchRemark
        {
            set;
            get;
        }

        public string InOutRemark
        {
            set;
            get;
        }

        public int FilterOrderBagId
        {
            get;
            set;
        }

        public string FilterShopName
        {
            get;
            set;
        }



        public string FilterCtmName
        {
            get;
            set;
        }

        public int FilterPriority
        {
            get;
            set;
        }

        public string FilterProductName
        {
            get;
            set;
        }

        public DateTime FilterStartOutTime
        {
            get;
            set;
        }

        public DateTime FilterEndOutTime
        {
            get;
            set;
        }

        public DateTime FilterStartPreFetchTime
        {
            get;
            set;
        }

        public DateTime FilterEndPriFetchTime
        {
            get;
            set;
        }

        public string OutOperateUser
        {
            get;
            set;
        }

        public int OutShopId
        {
            get;
            set;
        }




        public void FillQuickFilterCOMB(List<Tb_OrderBag_Info> listOrderBag, List<OrderInfo_Info> litOrderInfo, List<Tb_Product_Info> listProduct, List<OutProcessRecord_Info> listOutRecord, List<OutProcessShop_Info> listOutShop, List<string> listShopName)
        {
            comboBoxEdit_priority.Properties.Items.Clear();
            comboBoxEdit_orderId.Properties.Items.Clear();
            ComEdit_shop.Properties.Items.Clear();
            comboBoxEdit_ctm.Properties.Items.Clear();
            comboxeditproductType.Properties.Items.Clear();
            comboBoxEdit_outPreseon.Properties.Items.Clear();
            listOrderBag.Add(new Tb_OrderBag_Info() { BagName = "全部" });
            litOrderInfo.Add(new OrderInfo_Info() { CtmName = "全部" });
            listProduct.Add(new Tb_Product_Info() { ProductName = "全部" });
            listOutRecord.Add(new OutProcessRecord_Info() { CreateUser = "全部" });
            listOutShop.Add(new OutProcessShop_Info() { OutShopId = 0, OutShopName = "全部" });
            listShopName.Add("全部");
            foreach (var list in listOrderBag)
            {
                //if (!comboBoxEdit_orderId.Properties.Items.Contains(list.OrderBagId))
                //{

                //    this.comboBoxEdit_orderId.Properties.Items.Add(list.OrderBagId);
                //}
                if (!comboBoxEdit_priority.Properties.Items.Contains(list.Priority))
                {
                    comboBoxEdit_priority.Properties.Items.Add(list.Priority);
                }
            }


            foreach (string s in listShopName)
            {
                if (!ComEdit_shop.Properties.Items.Contains(s))
                {
                    ComEdit_shop.Properties.Items.Add(s);
                }
            }


            foreach (var list in litOrderInfo)
            {
                if (!comboBoxEdit_orderId.Properties.Items.Contains(list.OrderBagId))
                {
                    this.comboBoxEdit_orderId.Properties.Items.Add(list.OrderProductId);
                }
                if (!comboBoxEdit_ctm.Properties.Items.Contains(list.CtmName))
                {
                    comboBoxEdit_ctm.Properties.Items.Add(list.CtmName);
                }
            }
            foreach (Tb_Product_Info list in listProduct)
            {
                if (!comboxeditproductType.Properties.Items.Contains(list.ProductName))
                    comboxeditproductType.Properties.Items.Add(list.ProductName);
            }
            foreach (OutProcessRecord_Info info in listOutRecord)
            {
                if (info.CreateUser == null)
                {
                    continue;
                }
                if (!comboBoxEdit_outPreseon.Properties.Items.Contains(info.CreateUser))
                {
                    comboBoxEdit_outPreseon.Properties.Items.Add(info.CreateUser);
                }
            }
            lookUpEditoutShop.DataSource = listOutShop;
            lookUpEditoutShop.DisplayMember = "OutShopName";
            lookUpEditoutShop.ValueMember = "OutShopId";

        }


        void shopInfo_SetSelectedShop(int shopId)
        {
            foreach (Control uc in flowLayoutPanel_Shop.Controls)
            {
                ((UOutShopInfo)uc).BorderStyle = BorderStyle.FixedSingle;
                ((UOutShopInfo)uc).BackColor = Color.White;
            }
            this.SelectedOutShopId = shopId;
            //得到委外厂商信息绑定数据
            _presenter.SelectOutShop();
            SetHights();
        }
        private void SetHights()
        {
            //int intHight = flowLayoutPanel_Shop * 103+50;
            //this.splitContainerControl_Factory.Panel1.Height = intHight;

        }

        void shopInfo_ExecuteDrag(List<string> strList)
        {
            ///将选定的产品委外到选定厂商

            //foreach (string s in strList)
            //{
            //    SelectedOrderProductidList.Add(Convert.ToInt32(s));
            //}
            FrmInputInfo frmInputInfo = new FrmInputInfo();
            frmInputInfo.Send += new FrmInputInfo.SendMark(ExecuteAssignOut);
            frmInputInfo.ShowDialog();

        }
        private void ExecuteAssignOut(string str)
        {
            this.Cursor = Cursors.WaitCursor;
            this.InOutRemark = str;
            _presenter.AssignOutTask();
            // _presenter.SelectOutShop();
            SetHights();
            this.Cursor = Cursors.Default;
        }

        /// <summary>
        /// 获得当前委外厂商Id 并显示委外产品信息
        /// </summary>
        /// <param name="intShopId"></param>
        private void shopInfo_GetSelectedShopId(int intShopId)
        {
            this.SelectedOutShopId = intShopId;
            //得到委外厂商信息绑定数据
            // _presenter.SelectOutShop();
            SetHights();

        }

        #endregion

        private void gridView_Main_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            if (e.RowHandle == 0 && e.Clicks == 1)
            {

                // GetSelectMainProductId();
                if (SelectedOrderInfoStatus == 3)
                {
                    _presenter.ShowOutShopView();
                    // ShowOutShopView();
                    //this.SelectedOutShopId = _presenter.GetFristoutShopID();
                    //_presenter.SelectOutShop();

                }
            }


            //DisplayOutShopByProduct(listP);
            ///显示委外厂商信息
        }
        /// <summary>
        /// 取得选择的产品Id
        /// </summary>
        /// <returns></returns>
        private void GetSelectMainProductId()
        {
            if (!IsOuting)
            {
                //int[] rowId = gridView_Main.GetSelectedRows();
                int[] rowId = gridView_Main.GetSelectedRows();
                List<int> pruductId = new List<int>();
                foreach (int id in rowId)
                {
                    if (id >= 0)
                    {
                        int pId = Convert.ToInt32(((OverOrderView)(gridView_Main.GetRow(id))).OrderProductInfo.OrderProductId);
                        int strTemp = pruductId.Find(delegate(int a)
                        {
                            if (a == pId)
                            {
                                return true;
                            }
                            else
                            {
                                return false;
                            }
                        });

                        if (strTemp == 0)
                        {
                            pruductId.Add(pId);

                        }
                    }
                }
            }

            //SelectedOrderProductidList.Clear();
            //SelectedOrderProductidList = pruductId;



            //gridView_Main.ClearSelection();
            //gridView_Main.SelectRow(-1);

        }


        #region///托拽
        GridHitInfo downHitInfo = null;
        private void gridView_Main_MouseDown(object sender, MouseEventArgs e)
        {
            GridView view = sender as GridView;
            downHitInfo = null;
            GridHitInfo hitInfo = view.CalcHitInfo(new Point(e.X, e.Y));
            if (Control.ModifierKeys != Keys.None) return;
            if (e.Button == MouseButtons.Left && hitInfo.InRow && hitInfo.HitTest != GridHitTest.RowIndicator)
                downHitInfo = hitInfo;


            //GetSelectMainProductId();
            //if (SelectedOrderInfoStatus == 3)
            //{
            //    if (SelectedOrderProductidList.Count>=1)
            //    {
            //             _presenter.ShowOutShopView();
            //    // ShowOutShopView();
            //    _presenter.SelectOutShop();
            //    }

            //}


        }
        private void gridView_Main_MouseMove(object sender, MouseEventArgs e)
        {
            GridView view = sender as GridView;
            if (e.Button == MouseButtons.Left && downHitInfo != null)
            {
                _presenter.ShowOutShopView();
                Size dragSize = SystemInformation.DragSize;
                Rectangle dragRect = new Rectangle(new Point(downHitInfo.HitPoint.X - dragSize.Width / 2,
                    downHitInfo.HitPoint.Y - dragSize.Height / 2), dragSize);
                if (!dragRect.Contains(new Point(e.X, e.Y)))
                {
                    DoDragDrop(GetDragData(view), DragDropEffects.All);
                    downHitInfo = null;
                }
            }
        }
        private List<string> GetDragData(GridView view)
        {
            List<string> result = new List<string>();
            int[] selection = view.GetSelectedRows();
            if (selection == null) return null;
            int count = selection.Length;

            for (int i = 0; i < count; i++)
            {
                //if (view.GetRowCellDisplayText(selection[i], view.Columns["OrderInfosStatus"]).ToString().Equals("分片完成"))
                //{
                result.Add(view.GetRowCellDisplayText(selection[i], view.Columns["OrderProductId"]));
                //}
            }
            return result;
        }
        #endregion

        /// <summary>
        /// 取消委外
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void simpleButton_cancel_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            int[] rowId = gridView_product.GetSelectedRows();
            List<int> pruductId = new List<int>();
            foreach (int id in rowId)
            {
                if (id >= 0)
                {
                    int pId = Convert.ToInt32(((OverOrderView)(gridView_product.GetRow(id))).OrderProductInfo.OrderProductId);

                    int strTemp = pruductId.Find(delegate(int a)
                    {
                        if (a == pId)
                        {
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    });

                    if (strTemp == 0)
                    {
                        pruductId.Add(pId);

                    }
                    if (pId == 0)
                    {
                        pruductId.Add(pId);

                    }
                }
            }
            //SelectedOrderProductidList.Clear();
            //SelectedOrderProductidList = pruductId;

            _presenter.CancelTask();
            this.Cursor = Cursors.Default;
        }
        /// <summary>
        /// 收件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void simpleButton_ReceiveOrder_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            GetSelectMainProductId();
            FrmInputInfo frmInputInfo = new FrmInputInfo();
            frmInputInfo.Send += new FrmInputInfo.SendMark(frmInputInfo_Send);
            frmInputInfo.ShowDialog();
            this.Cursor = Cursors.Default;

        }
        /// <summary>
        /// 结单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void simpleButton_OverOrder_Click(object sender, EventArgs e)
        {
            GetSelectMainProductId();

            _presenter.OverOrder();
        }

        void frmInputInfo_Send(string strMark)
        {
            this.InFetchRemark = strMark;
            _presenter.FetchAndOrderOrder();
        }
        private void ShowMainColums()
        {
            switch (SelectedOrderInfoStatus)
            {
                case (int)OrderInfoStatus.FenPianCompleted:
                    {
                        gridView_Main.Columns["OutShopName"].Visible = false;
                        gridView_Main.Columns["CreateTime"].Visible = false;
                        gridView_Main.Columns["OutRemark"].Visible = false;
                        gridView_Main.Columns["PreFetchTime"].Visible = false;
                        gridView_Main.Columns["FetchTime"].Visible = false;
                        gridView_Main.Columns["FetchRemark"].Visible = false;
                        gridView_Main.Columns["CreateUserName"].Visible = false;

                    }
                    break;
                case (int)OrderInfoStatus.OutProcess:
                    {
                        gridView_Main.Columns["OutShopName"].Visible = true;
                        gridView_Main.Columns["CreateTime"].Visible = true;
                        gridView_Main.Columns["OutRemark"].Visible = true;
                        gridView_Main.Columns["PreFetchTime"].Visible = false;
                        gridView_Main.Columns["FetchTime"].Visible = true;
                        gridView_Main.Columns["FetchRemark"].Visible = false;
                        gridView_Main.Columns["CreateUserName"].Visible = false;

                    }
                    break;
                default:
                    break;
            }
        }
        private bool IsOuting = false;
        private void comboBoxEdit_type_Closed(object sender, DevExpress.XtraEditors.Controls.ClosedEventArgs e)
        {
            if (comboBoxEdit_type.SelectedItem.ToString().Equals("分片完成"))
            {
                IsOuting = false;
                this.splitContainerControl_Main.PanelVisibility = SplitPanelVisibility.Both;
                this.SelectedOrderInfoStatus = 3;
                //simpleButton_OutProduct.Visible = true;
                ////simpleButton_OverOrder.Visible = true;
                //simpleButton_ReceiveOrder.Visible = false;
                simpleButton_CancelOut.Visible = false;


                //labelControl6.Visible = false;
                comboBoxEdit_outPreseon.Visible = true;
                //labelControl7.Visible = false;
                lookUpEditoutShop.Visible = true;
                labelControl8.Visible = false;
                labelControl10.Visible = false;
                dateEdit_OutStart.Visible = false;
                dateEdit_inStart.Visible = false;
                dateEditOutEnd.Visible = false;
                dateEdit_InEnd.Visible = false;
                labelControl9.Visible = false;
                labelControl11.Visible = false;
                simpleButton_reflush.Visible = true;

                //comboBox_OrderProduct.Visible = false;
                //comboBoxProduct.Visible = false;
                //comboBoxShop.Visible = false;
                //comboBoxOutUser.Visible = false;
                //comboBoxctmName.Visible = false;
                //comboBox_OutShop.Visible = false;
                //comboBoxtPriority.Visible = false;

                labelControl1.Visible = false;
                labelContro_productType.Visible = false;
                labelControl2.Visible = false;
                labelControl5.Visible = false;
                labelControl4.Visible = false;
                labelControl6.Visible = false;
                labelControl7.Visible = false;

                comboBox_OrderProduct.Visible = false;
                comboBoxEdit_orderId.Visible = false;
                comboBoxProduct.Visible = false;
                comboxeditproductType.Visible = false;
                comboBoxShop.Visible = false;
                ComEdit_shop.Visible = false;
                comboBoxOutUser.Visible = false;
                comboBoxEdit_outPreseon.Visible = false;
                comboBoxctmName.Visible = false;
                comboBoxEdit_ctm.Visible = false;
                comboBox_OutShop.Visible = false;
                lookUpEditoutShop.Visible = false;
                comboBoxtPriority.Visible = false;
                comboBoxEdit_priority.Visible = false;
                button1.Visible = false;
                OutStatus.Visible = false;
                _presenter.GetMainTask();
            }
            else if (comboBoxEdit_type.SelectedItem.ToString().Equals("外加工中"))
            {
                IsOuting = true;
                //simpleButton_CancelOut.Visible = false;
                this.splitContainerControl_Main.PanelVisibility = SplitPanelVisibility.Panel1;
                this.SelectedOrderInfoStatus = 4;
                //simpleButton_ReceiveOrder.Visible = true;
                //simpleButton_OutProduct.Visible = false;
                //simpleButton_OverOrder.Visible = false;
                simpleButton_CancelOut.Visible = true;

                labelControl6.Visible = true;
                comboBoxEdit_outPreseon.Visible = true;
                labelControl7.Visible = true;
                lookUpEditoutShop.Visible = true;
                //labelControl8.Visible = true;
                //labelControl10.Visible = true;
                //dateEdit_OutStart.Visible = true;
                //dateEdit_inStart.Visible = true;
                //dateEditOutEnd.Visible = true;
                //dateEdit_InEnd.Visible = true;
                //labelControl9.Visible = true;
                //labelControl11.Visible = true;
                //_presenter.GetMainTask();
                simpleButton_reflush.Visible = false;

                //labelControl1.Visible = false;
                //labelContro_productType.Visible = false;
                //labelControl2.Visible = false;
                //labelControl5.Visible = false;
                //labelControl4.Visible = false;
                //labelControl6.Visible = false;
                //labelControl7.Visible = false;

                ////simpleButton_CancelOut.Visible = false;
                //comboBox_OrderProduct.Visible = false;
                //comboBoxEdit_orderId.Visible = false;
                //comboBoxProduct.Visible = false;
                //comboxeditproductType.Visible = false;
                //comboBoxShop.Visible = false;
                //ComEdit_shop.Visible = false;
                //comboBoxOutUser.Visible = false;
                //comboBoxEdit_outPreseon.Visible = false;
                //comboBoxctmName.Visible = false;
                //comboBoxEdit_ctm.Visible = false;
                //comboBox_OutShop.Visible = false;
                //lookUpEditoutShop.Visible = false;
                //comboBoxtPriority.Visible = false;
                //comboBoxEdit_priority.Visible = false;
                DataTable table = _presenter.GetOutingTask();
                gridControlMain.DataSource = table;
                button1.Visible = true;
                //ShortBing();
                //comboBox_OrderProduct.SelectedIndex = -1;
                //comboBoxProduct.SelectedIndex = -1;
                //comboBoxOutUser.SelectedIndex = -1;
                //comboBoxShop.SelectedIndex = -1;
                //comboBoxctmName.SelectedIndex = -1;
                //comboBox_OutShop.SelectedIndex = -1;
                //comboBoxtPriority.SelectedIndex = -1;
                OutStatus.Visible = true;

            }
            ShowMainColums();

        }
        OverOrderBLL _bll = new OverOrderBLL();
        private void ShortBing()
        {

            DataTable orderProduct = _bll.GetOrderProductCode();
            comboBox_OrderProduct.DataSource = orderProduct;
            comboBox_OrderProduct.DisplayMember = "OrderProductCode";
            comboBox_OrderProduct.ValueMember = "OrderProductCode";

            DataTable Product = _bll.GetProductName();
            comboBoxProduct.DataSource = Product;
            comboBoxProduct.DisplayMember = "ProductName";
            comboBoxProduct.ValueMember = "ProductName";

            DataTable shopTable = _bll.GetShop();
            comboBoxShop.DataSource = shopTable;
            comboBoxShop.DisplayMember = "ShopName";
            comboBoxShop.ValueMember = "ShopId";

            DataTable outUserTable = _bll.GetCreateUser();
            comboBoxOutUser.DataSource = outUserTable;
            comboBoxOutUser.ValueMember = "UserName";
            comboBoxOutUser.DisplayMember = "UserName";

            DataTable outShopTable = _bll.GetOutShop();
            comboBox_OutShop.DataSource = outShopTable;
            comboBox_OutShop.DisplayMember = "OutShopName";
            comboBox_OutShop.ValueMember = "OutShopName";

            DataTable PriorityTable = _bll.GetPriority();
            comboBoxtPriority.DataSource = PriorityTable;
            comboBoxtPriority.DisplayMember = "Priority";
            comboBoxtPriority.ValueMember = "Priority";


        }
        private void gridView_Main_RowCellStyle(object sender, RowCellStyleEventArgs e)
        {
            //try
            //{
            //    if (e.RowHandle >= 0)
            //    {
            //        if (e.Column.Name == "OrderInfosStatus")
            //        {
            //            int status =

            //                Convert.ToInt32(
            //                    ((OverOrderView)(gridView_Main.GetRow(e.RowHandle))).OrderProductInfo.Status);
            //            switch (status)
            //            {
            //                case (int)OrderInfoStatus.FenPianCompleted:
            //                    {
            //                        gridView_Main.SetRowCellValue(e.RowHandle, "BagName", "分片完成");
            //                        if (e.Column != null) e.Column.Dispose();
            //                    }
            //                    break;
            //                case (int)OrderInfoStatus.OutProcess:
            //                    {
            //                        //gridView_Main.SetRowCellValue(e.RowHandle, "OrderInfosStatus", "外加工中");
            //                    }
            //                    break;
            //                default:
            //                    break;
            //            }
            //        }
            //    }
            //}
            //catch (Exception)
            //{

            //    throw;
            //}

        }



        private void gridView_Main_CustomDrawCell(object sender, RowCellCustomDrawEventArgs e)
        {
            if (e.Column.FieldName == "OrderInfosStatus")
            {
                int status =

                        Convert.ToInt32(
                            ((OverOrderView)(gridView_Main.GetRow(e.RowHandle))).OrderProductInfo.Status);
                switch (status)
                {
                    case (int)OrderInfoStatus.FenPianCompleted:
                        {
                            //gridView_Main.SetRowCellValue(e.RowHandle, "BagName", "分片完成");
                            //if (e.Column != null) e.Column.Dispose();
                            e.DisplayText = "分片完成";
                        }
                        break;
                    case (int)OrderInfoStatus.OutProcess:
                        {
                            //gridView_Main.SetRowCellValue(e.RowHandle, "OrderInfosStatus", "外加工中");
                            e.DisplayText = "外加工中";
                        }
                        break;
                    default:
                        break;
                }
            }
        }

        private void gridView_product_CustomDrawCell(object sender, RowCellCustomDrawEventArgs e)
        {
            if (e.Column.FieldName == "OrderInfosStatus")
            {
                int status =

                        Convert.ToInt32(
                            ((OverOrderView)(gridView_product.GetRow(e.RowHandle))).OrderInfosStatus);
                switch (status)
                {
                    case (int)OrderInfoStatus.FenPianCompleted:
                        {
                            //gridView_Main.SetRowCellValue(e.RowHandle, "BagName", "分片完成");
                            //if (e.Column != null) e.Column.Dispose();
                            e.DisplayText = "分片完成";
                        }
                        break;
                    case (int)OrderInfoStatus.OutProcess:
                        {
                            //gridView_Main.SetRowCellValue(e.RowHandle, "OrderInfosStatus", "外加工中");
                            e.DisplayText = "外加工中";
                        }
                        break;
                    default:
                        break;
                }
            }
        }

        private void gridView_Main_CustomDrawRowIndicator(object sender, RowIndicatorCustomDrawEventArgs e)
        {
            e.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;

            if (e.Info.IsRowIndicator && e.RowHandle >= 0)
            {
                e.Info.DisplayText = (e.RowHandle + 1).ToString().Trim();

            }
        }

        private void gridView_product_CustomDrawRowIndicator(object sender, RowIndicatorCustomDrawEventArgs e)
        {
            e.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;

            if (e.Info.IsRowIndicator && e.RowHandle >= 0)
            {
                e.Info.DisplayText = (e.RowHandle + 1).ToString().Trim();

            }
        }

        private void gridView_Main_RowStyle(object sender, RowStyleEventArgs e)
        {
            if (e.RowHandle >= 0)
            {
                if (e.RowHandle % 2 == 0)
                {
                    e.Appearance.BackColor = Color.LightGray;
                }
            }
        }

        private void labelControl7_Click(object sender, EventArgs e)
        {

        }

        private void comboBoxEdit_orderId_Closed(object sender, DevExpress.XtraEditors.Controls.ClosedEventArgs e)
        {
            if (comboBoxEdit_orderId.Text.Trim() != "")
            {
                this.FilterOrderBagId = Convert.ToInt32(comboBoxEdit_orderId.SelectedItem.ToString());
                _presenter.FilterOrderBag();
            }
        }



        private void comboBoxEdit_ctm_Closed(object sender, DevExpress.XtraEditors.Controls.ClosedEventArgs e)
        {
            this.FilterCtmName = comboBoxEdit_ctm.Text;
            _presenter.FilterCtmName();
        }

        private void comboBoxEdit_priority_Closed(object sender, DevExpress.XtraEditors.Controls.ClosedEventArgs e)
        {
            if (comboBoxEdit_priority.Text.Trim() != "")
            {
                this.FilterPriority = Convert.ToInt32(comboBoxEdit_priority.Text);
                _presenter.FilterPriority();
            }
        }

        private void comboxeditproductType_Closed(object sender, DevExpress.XtraEditors.Controls.ClosedEventArgs e)
        {
            this.FilterProductName = comboxeditproductType.Text;
            _presenter.FilterProduct();
        }

        private void comboBoxEdit_outPreseon_Closed(object sender, DevExpress.XtraEditors.Controls.ClosedEventArgs e)
        {
            this.OutOperateUser = comboBoxEdit_outPreseon.Text;
            _presenter.FilterOutOperateUser();
        }

        private void lookUpEditoutShop_Closed(object sender, DevExpress.XtraEditors.Controls.ClosedEventArgs e)
        {




        }

        private void simpleButton_reflush_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            //_presenter = new MINERP.OverOrder.Presenter.FrmOverOrderPresenter(this);
            _presenter.GetMainTask();
            this.Cursor = Cursors.Default;
        }

        private void dateEditOutEnd_Closed(object sender, DevExpress.XtraEditors.Controls.ClosedEventArgs e)
        {
            this.FilterStartOutTime = dateEdit_OutStart.DateTime;
            this.FilterEndOutTime = dateEditOutEnd.DateTime;
            _presenter.FilterEndOutTime();
        }

        private void dateEdit_InEnd_Closed(object sender, DevExpress.XtraEditors.Controls.ClosedEventArgs e)
        {
            this.FilterStartPreFetchTime = dateEdit_inStart.DateTime;
            this.FilterEndPriFetchTime = dateEdit_InEnd.DateTime;
            _presenter.FilterEndPreFetchTime();
        }

        private void ComEdit_shop_Closed(object sender, DevExpress.XtraEditors.Controls.ClosedEventArgs e)
        {
            this.FilterShopName = ComEdit_shop.Text;
            _presenter.FilterShop();
        }

        private void simpleButton_CancelOut_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            //GetSelectMainProductId();

            _presenter.CancelTask1();
            DataTable table = _presenter.GetOutingTask();
            gridControlMain.DataSource = table;
            this.Cursor = Cursors.Default;
        }

        private void gridControlMain_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(typeof(List<int>)))
            {
                List<int> obj = (List<int>)e.Data.GetData(typeof(List<int>));
                if (obj.Count > 0)
                {
                    e.Effect = DragDropEffects.Copy;

                }
            }

        }
        #region //拖拽取消任务
        private void gridView_product_MouseDown(object sender, MouseEventArgs e)
        {
            GridView view = sender as GridView;
            downHitInfo = null;
            GridHitInfo hitInfo = view.CalcHitInfo(new Point(e.X, e.Y));
            if (Control.ModifierKeys != Keys.None) return;
            if (e.Button == MouseButtons.Left && hitInfo.InRow && hitInfo.HitTest != GridHitTest.RowIndicator)
                downHitInfo = hitInfo;
        }

        private void gridView_product_MouseMove(object sender, MouseEventArgs e)
        {
            GridView view = sender as GridView;
            if (e.Button == MouseButtons.Left && downHitInfo != null)
            {
                Size dragSize = SystemInformation.DragSize;
                Rectangle dragRect = new Rectangle(new Point(downHitInfo.HitPoint.X - dragSize.Width / 2,
                    downHitInfo.HitPoint.Y - dragSize.Height / 2), dragSize);
                if (!dragRect.Contains(new Point(e.X, e.Y)))
                {
                    DoDragDrop(GetDragDataCancel(view), DragDropEffects.All);
                    downHitInfo = null;
                }
            }
        }
        private List<int> GetDragDataCancel(GridView view)
        {
            List<int> result = new List<int>();
            int[] selection = view.GetSelectedRows();
            if (selection == null) return null;
            int count = selection.Length;

            for (int i = 0; i < count; i++)
            {
                //if (view.GetRowCellDisplayText(selection[i], view.Columns["OrderInfosStatus"]).ToString().Equals("分片完成"))
                //{
                result.Add(Convert.ToInt32(view.GetRowCellDisplayText(selection[i], view.Columns["OrderProductId"])));
                //}
            }
            return result;

        }

        private void gridControlMain_DragDrop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(typeof(List<int>)))
            {
                List<int> obj = (List<int>)e.Data.GetData(typeof(List<int>));
                if (obj.Count > 0)
                {
                    e.Effect = DragDropEffects.Copy;
                    //this.SelectedOrderProductidList = obj;
                    _presenter.CancelTask();

                }

            }
        }
        #endregion


        #region IFrmOverOrder 成员


        public int CurTechId
        {
            get
            {
                return base.CurrentTechID;
                //return 1;
            }
            set { ;}

        }

        #endregion

        private void FrmOverOrder_FormClosed(object sender, FormClosedEventArgs e)
        {
            Logoff.ShowMainFrm();
        }

        #region IFrmOverOrder 成员


        public List<int> SelectedCancelProductList
        {
            get
            {
                List<int> orderInfoIdList = new List<int>();
                int pId;
                foreach (int rowIndex in gridView_product.GetSelectedRows())
                {
                    pId = Convert.ToInt32(((OverOrderView)(gridView_product.GetRow(rowIndex))).OrderProductInfo.OrderProductId);
                    orderInfoIdList.Add(pId);
                }
                return orderInfoIdList;
            }
        }

        #endregion

        private void dateEdit_OutStart_Closed(object sender, DevExpress.XtraEditors.Controls.ClosedEventArgs e)
        {
            this.FilterStartOutTime = dateEdit_OutStart.DateTime;
            _presenter.FilterStartOutTime();
        }

        private void dateEdit_inStart_Closed(object sender, DevExpress.XtraEditors.Controls.ClosedEventArgs e)
        {
            this.FilterStartPreFetchTime = dateEdit_inStart.DateTime;
            _presenter.FilterStartPreFetchTime();
        }

        private void lookUpEditoutShop_SelectionChangeCommitted(object sender, EventArgs e)
        {
            this.OutShopId = Convert.ToInt32(((OutProcessShop_Info)lookUpEditoutShop.SelectedItem).OutShopId);
            _presenter.FilterOutShop();
        }

        private void comboBoxEdit_orderId_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                if (comboBoxEdit_orderId.Text.Trim() != "")
                {
                    try
                    {
                        this.FilterOrderBagId = Convert.ToInt32(comboBoxEdit_orderId.SelectedItem.ToString());
                    }
                    catch (Exception)
                    {
                        MessageBox.Show("请输入数字，并重试！");
                        return;
                    }

                    _presenter.FilterOrderBag();
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //_presenter.ShowHCView();
            comboBox_OrderProduct.SelectedIndex = -1;
            comboBoxProduct.SelectedIndex = -1;
            comboBoxOutUser.SelectedIndex = -1;
            comboBoxShop.SelectedIndex = -1;
            comboBoxctmName.SelectedIndex = -1;
            comboBox_OutShop.SelectedIndex = -1;
            comboBoxtPriority.SelectedIndex = -1;
            DataTable table = _presenter.GetOutingTask();
            gridControlMain.DataSource = table;
        }

        private void btnexport_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveExportDialog = new SaveFileDialog();
            saveExportDialog.Filter = "文本文件 (*.txt)|*.txt|pdf(*.pdf)|*.pdf|电子表格(*.xls)|*.xls|html(*.html)|*.html";
            saveExportDialog.FilterIndex = 3;
            saveExportDialog.RestoreDirectory = true;

            //this.gridViewOrder.OptionsPrint.PrintDetails = true;
            //this.gridViewOrder.OptionsPrint.ExpandAllDetails = true;
            //this.gridViewOrder.OptionsPrint.ExpandAllGroups = true;
            try
            {

                if (saveExportDialog.ShowDialog() == DialogResult.OK)
                {
                    string fileName = saveExportDialog.FileName;

                    if (saveExportDialog.OpenFile() != null)
                    {
                        switch (saveExportDialog.FilterIndex)
                        {
                            case 3:
                                this.gridView_Main.ExportToXls(fileName);
                                return;
                            case 4:
                                this.gridView_Main.ExportToHtml(fileName);
                                return;
                            case 2:
                                this.gridView_Main.ExportToPdf(fileName);
                                return;
                            case 1:
                                this.gridView_Main.ExportToText(fileName);
                                return;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
        }



        private void Commit()
        {
            //string barcode = comboBox_OrderProduct.SelectedItem.ToString().Trim();
            //if (barcode != null && barcode != "")
            //{
            //    DataTable tableBarCode = _bll.GetOuttingTaskByBarCode(barcode);
            //}

            //if()
            DataTable table = _presenter.GetOutingTask();
            DataTable tableTemp = table.Clone();
            tableTemp.Clear();
            string barcode = comboBox_OrderProduct.SelectedValue == null ? "" : comboBox_OrderProduct.SelectedValue.ToString();
            DataRow[] rows;
            if (barcode != null && barcode != "")
            {
                rows = table.Select("OrderProductCode='" + barcode + "'");
                foreach (DataRow r in rows)
                {
                    tableTemp.ImportRow(r);
                }
                gridControlMain.DataSource = tableTemp;
                return;
            }
            string Sql = " 1=1";
            string shopName = "";
            DataRowView view = (DataRowView)comboBoxShop.SelectedItem;
            if (view != null)
            {
                shopName = (view[1].ToString());
            }
            string CtmName = "";
            DataRowView viewctm = (DataRowView)comboBoxctmName.SelectedItem;
            if (viewctm != null)
            {
                CtmName = viewctm[0].ToString();
            }

            string productName = comboBoxProduct.SelectedValue == null ? "" : comboBoxProduct.SelectedValue.ToString();
            //shopName= comboBoxShop.SelectedItem == null ? "" : comboBoxShop.SelectedItem.ToString();
            string OutUser = comboBoxOutUser.SelectedValue == null ? "" : comboBoxOutUser.SelectedValue.ToString();
            //string CtmName = comboBoxctmName.SelectedValue==null?"":comboBoxctmName.SelectedValue.ToString();
            string OutShop = comboBox_OutShop.SelectedValue == null ? "" : comboBox_OutShop.SelectedValue.ToString();
            string Priority = comboBoxtPriority.SelectedValue == null ? "" : comboBoxtPriority.SelectedValue.ToString();
            if (productName != null && productName != "")
            {
                Sql += " and ProductName='" + productName + "'";
            }
            if (shopName != "" && shopName != null)
            {
                Sql += " and ShopName='" + shopName + "'";
            }
            if (OutUser != "" && OutUser != null)
            {
                Sql += " and UserName='" + OutUser + "'";
            }
            if (CtmName != "" && CtmName != null)
            {
                Sql += " and CtmName='" + CtmName + "'";
            }
            if (OutShop != "" && OutShop != null)
            {
                Sql += " and OutShopName='" + OutShop + "'";
            }
            if (Priority != "" && Priority != null)
            {
                Sql += " and Priority='" + Priority + "'";
            }
            rows = table.Select(Sql);
            foreach (DataRow r in rows)
            {
                tableTemp.ImportRow(r);
            }
            gridControlMain.DataSource = tableTemp;

        }






        private void comboBox_OrderProduct_SelectedIndexChanged(object sender, EventArgs e)
        {
            Commit();
        }

        private void comboBoxShop_SelectedIndexChanged(object sender, EventArgs e)
        {
            Commit();
            if (comboBoxShop.SelectedItem == null)
            {
                return;
            }
            int shopid = 0;
            DataRowView view = (DataRowView)comboBoxShop.SelectedItem;
            if (view != null)
            {
                shopid = Convert.ToInt32((view[0].ToString()));
            }
            //shopid = Convert.ToInt32(comboBoxShop.SelectedValue.ToString());
            DataTable ctmTable = _bll.GetCtmName(shopid);
            comboBoxctmName.DataSource = ctmTable;
            comboBoxctmName.DisplayMember = "CtmName";
            comboBoxctmName.SelectedIndex = -1;
        }

        private void comboBoxProduct_SelectedIndexChanged(object sender, EventArgs e)
        {
            Commit();
        }

        private void comboBoxOutUser_SelectedIndexChanged(object sender, EventArgs e)
        {
            Commit();
        }

        private void comboBoxctmName_SelectedIndexChanged(object sender, EventArgs e)
        {
            Commit();
        }

        private void comboBox_OutShop_SelectedIndexChanged(object sender, EventArgs e)
        {
            Commit();
        }

        private void comboBoxtPriority_SelectedIndexChanged(object sender, EventArgs e)
        {
            Commit();
        }

        private void buttonEmail_Click(object sender, EventArgs e)
        {
            FrmEmaiSys frm = new FrmEmaiSys();
            frm.ShowDialog();
        }

        private void gridView_Main_FocusedRowChanged(object sender, FocusedRowChangedEventArgs e)
        {
            if (e.FocusedRowHandle > 0)
            {
                //gridView_Main.

                if (SelectedOrderInfoStatus == 3)
                {
                    _presenter.ShowOutShopView();
                    // ShowOutShopView();
                    //this.SelectedOutShopId = _presenter.GetFristoutShopID();
                    //_presenter.SelectOutShop();

                }
                //}
            }
        }

        private void buttonEmail_Click_1(object sender, EventArgs e)
        {
            FrmEmaiSys frm = new FrmEmaiSys();
            frm.ShowDialog();
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveExportDialog = new SaveFileDialog();
            saveExportDialog.Filter = "文本文件 (*.txt)|*.txt|pdf(*.pdf)|*.pdf|电子表格(*.xls)|*.xls|html(*.html)|*.html";
            saveExportDialog.FilterIndex = 3;
            saveExportDialog.RestoreDirectory = true;

            //this.gridViewOrder.OptionsPrint.PrintDetails = true;
            //this.gridViewOrder.OptionsPrint.ExpandAllDetails = true;
            //this.gridViewOrder.OptionsPrint.ExpandAllGroups = true;
            try
            {

                if (saveExportDialog.ShowDialog() == DialogResult.OK)
                {
                    string fileName = saveExportDialog.FileName;

                    if (saveExportDialog.OpenFile() != null)
                    {
                        switch (saveExportDialog.FilterIndex)
                        {
                            case 3:
                                this.gridView_Main.ExportToXls(fileName);
                                return;
                            case 4:
                                this.gridView_Main.ExportToHtml(fileName);
                                return;
                            case 2:
                                this.gridView_Main.ExportToPdf(fileName);
                                return;
                            case 1:
                                this.gridView_Main.ExportToText(fileName);
                                return;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
        }








    }
}
